package com.mybrothers.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mybrothers.po.UserInfo;
import com.mybrothers.service.UserService;

/**
 *  
 * @author Administrator
 *
 */
@Controller  
@RequestMapping("/user")  
public class UserController {  
	public Logger logger = LoggerFactory.getLogger(this.getClass().getName());
    @Autowired  
    private UserService userService;  
      
//    @RequestMapping("/showInfo/{userId}") 
    //本方法将处理/user/showInfo?userId=1,同时只处理get方法请求的路径
    @RequestMapping(value="/showInfo",method=RequestMethod.GET)
//    public String showUserInfo(ModelMap modelMap, @PathVariable int userId){  
    public String showUserInfo(@RequestParam("userId") int userId,Model model){
    	UserInfo userInfo = userService.getUserById(userId);  
//        modelMap.addAttribute("userInfo", userInfo); 
    	model.addAttribute(userInfo);
        logger.info("根据ID获得UserInfo："+userInfo.toString());
        return "/user/showInfo";  
    }  
    
    //本方法将处理/user/showInfo2/{userId},同时只处理get方法请求的路径
    @RequestMapping(value="/showInfo2",method=RequestMethod.GET)
    public String showUserInfo2(Map<String,Object> model, @PathVariable("userId") int userId){
    	UserInfo userInfo = userService.getUserById(userId);  
    	model.put("userInfo",userInfo);
        logger.info("根据ID获得UserInfo："+userInfo.toString());
        return "/user/showInfo";  
    }  
    
    //本方法将处理/user/showInfo3?userId=1,同时只处理get方法请求的路径
    @RequestMapping("/showInfo3")
    public String showUserInfo3(HttpServletRequest request){
    	Integer userId = Integer.parseInt(request.getParameter("userId"));
    	UserInfo userInfo = userService.getUserById(userId);  
    	request.setAttribute("userInfo", userInfo);
        logger.info("根据ID获得UserInfo："+userInfo.toString());
        return "/user/showInfo";  
    }
      
    @RequestMapping("/showInfos")  
    public @ResponseBody Object showUserInfos(){  
        List<UserInfo> userInfos = userService.getUsers();  
        return userInfos;  
    }  
}